package cn.tedu.tmall.admin.content.controller;

import cn.tedu.tmall.admin.content.pojo.param.ArticleAddParam;
import cn.tedu.tmall.admin.content.pojo.vo.ArticleItemListVO;
import cn.tedu.tmall.admin.content.pojo.vo.ArticleVO;
import cn.tedu.tmall.admin.content.serivce.IArticleService;
import cn.tedu.tmall.common.pojo.CurrentUser;
import cn.tedu.tmall.common.pojo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 管理端文章管理相关接口
 */
@Api(tags = "文章管理相关接口")
@Validated
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    /**
     * 增加文章接口
     *
     * @param articleAddParam 文章详情
     */
    @ApiOperation("增加文章")
    @PostMapping("/add")
    public JsonResult addArticle(ArticleAddParam articleAddParam) {
        //JsonResult jsonResult = new JsonResult();
        articleService.addArticle(articleAddParam);
        return JsonResult.ok();
    }


    @ApiOperation("文章列表")
    @PostMapping("/list/")
    @PreAuthorize("hasAuthority('/article/list')")
    public JsonResult listArticle(@RequestParam("categoryId")Long categoryId,
                                  @RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize")@Range(min=1,max=20, message = "页数太大") Integer pageSize) {
        PageData<ArticleItemListVO> articleVOS = articleService.listArticle(categoryId,pageNum,pageSize);
        return JsonResult.ok(articleVOS);
    }

    /**
     * 通过类别id查询文章列表
     */
    @ApiOperation("文章详情")
    @PostMapping("/detail")
    public JsonResult detail(Long articleId) {
        ArticleVO article =  articleService.detail(articleId);
        return JsonResult.ok(article);
    }
    @GetMapping("/current/user")
    @ApiOperation("识别当事人")
    public String principal(@ApiIgnore @AuthenticationPrincipal CurrentUser user) {
        return "成功处理请求：需要通过认证，且识别当事人：" + user;
    }
}